package com.teacher.game.model;

import android.graphics.Bitmap;
import android.graphics.Rect;
import com.teacher.game.framework.util.Painter;

/**
 * 道具系统 - 游戏中的各种增强道具
 * 包括加分道具、护盾道具、时间减缓等特殊效果
 */
public class PowerUp {
    
    // 道具类型常量
    public static final int TYPE_COIN = 0;       // 金币（加分）
    public static final int TYPE_SHIELD = 1;     // 护盾（免疫一次碰撞）
    public static final int TYPE_DOUBLE_SCORE = 2; // 双倍得分
    public static final int TYPE_SLOW_TIME = 3;   // 时间减缓
    
    // 道具属性
    private float x, y;              // 位置坐标
    private int type;                // 道具类型
    private int width, height;       // 尺寸
    private boolean collected;       // 是否已收集
    private float animationTimer;    // 动画计时器
    private int value;              // 道具价值（分数或效果强度）
    
    // 视觉效果
    private float pulseScale = 1.0f; // 脉动缩放
    private boolean pulsing = true;  // 是否正在脉动
    
    /**
     * 构造函数
     * @param x X坐标
     * @param y Y坐标
     * @param type 道具类型
     */
    public PowerUp(float x, float y, int type) {
        this.x = x;
        this.y = y;
        this.type = type;
        this.width = 32;
        this.height = 32;
        this.collected = false;
        this.animationTimer = 0;
        
        // 根据类型设置价值
        switch (type) {
            case TYPE_COIN:
                this.value = 100; // 金币100分
                break;
            case TYPE_SHIELD:
                this.value = 1; // 护盾持续时间/次数
                break;
            case TYPE_DOUBLE_SCORE:
                this.value = 5; // 双倍得分持续5秒
                break;
            case TYPE_SLOW_TIME:
                this.value = 3; // 时间减缓持续3秒
                break;
            default:
                this.value = 50;
        }
    }
    
    /**
     * 更新道具状态
     * @param delta 时间增量
     * @param speed 移动速度
     */
    public void update(float delta, int speed) {
        if (collected) return;
        
        // 水平移动
        x += speed * delta;
        
        // 更新动画
        animationTimer += delta;
        
        // 脉动效果
        if (pulsing) {
            pulseScale = 1.0f + (float) Math.sin(animationTimer * 4) * 0.1f;
        }
        
        // 上下浮动效果（仅限部分道具）
        if (type == TYPE_COIN || type == TYPE_DOUBLE_SCORE) {
            y += (float) Math.sin(animationTimer * 3) * 10 * delta;
        }
    }
    
    /**
     * 渲染道具
     * @param g 绘制工具
     */
    public void render(Painter g) {
        if (collected) return;
        
        // 根据道具类型选择颜色和形状
        int color = getTypeColor();
        g.setColor(color);
        
        // 应用脉动缩放
        int renderWidth = (int) (width * pulseScale);
        int renderHeight = (int) (height * pulseScale);
        int renderX = (int) (x - (renderWidth - width) / 2);
        int renderY = (int) (y - (renderHeight - height) / 2);
        
        // 绘制道具（简单的几何形状）
        switch (type) {
            case TYPE_COIN:
                // 金币 - 黄色圆形
                g.fillOval(renderX, renderY, renderWidth, renderHeight);
                g.setColor(android.graphics.Color.rgb(255, 215, 0)); // 金色边框
                drawBorder(g, renderX, renderY, renderWidth, renderHeight, true);
                break;
                
            case TYPE_SHIELD:
                // 护盾 - 蓝色盾牌形状
                g.fillOval(renderX, renderY, renderWidth, renderHeight);
                g.setColor(android.graphics.Color.WHITE);
                g.fillOval(renderX + 4, renderY + 4, renderWidth - 8, renderHeight - 8);
                break;
                
            case TYPE_DOUBLE_SCORE:
                // 双倍得分 - 绿色星形
                g.fillRect(renderX, renderY, renderWidth, renderHeight);
                g.setColor(android.graphics.Color.WHITE);
                drawCross(g, renderX, renderY, renderWidth, renderHeight);
                break;
                
            case TYPE_SLOW_TIME:
                // 时间减缓 - 紫色钟表
                g.fillOval(renderX, renderY, renderWidth, renderHeight);
                g.setColor(android.graphics.Color.WHITE);
                drawClock(g, renderX + renderWidth/2, renderY + renderHeight/2);
                break;
        }
    }
    
    /**
     * 获取道具类型对应的颜色
     */
    private int getTypeColor() {
        switch (type) {
            case TYPE_COIN:
                return android.graphics.Color.YELLOW;
            case TYPE_SHIELD:
                return android.graphics.Color.BLUE;
            case TYPE_DOUBLE_SCORE:
                return android.graphics.Color.GREEN;
            case TYPE_SLOW_TIME:
                return android.graphics.Color.MAGENTA;
            default:
                return android.graphics.Color.GRAY;
        }
    }
    
    /**
     * 绘制边框
     */
    private void drawBorder(Painter g, int x, int y, int w, int h, boolean circle) {
        if (circle) {
            // 简化的圆形边框
            g.fillOval(x - 2, y - 2, w + 4, h + 4);
        } else {
            g.fillRect(x - 2, y - 2, w + 4, 2); // 上
            g.fillRect(x - 2, y + h, w + 4, 2); // 下
            g.fillRect(x - 2, y - 2, 2, h + 4); // 左
            g.fillRect(x + w, y - 2, 2, h + 4); // 右
        }
    }
    
    /**
     * 绘制十字
     */
    private void drawCross(Painter g, int x, int y, int w, int h) {
        int crossSize = w / 4;
        g.fillRect(x + w/2 - 1, y + crossSize, 2, h - crossSize * 2);
        g.fillRect(x + crossSize, y + h/2 - 1, w - crossSize * 2, 2);
    }
    
    /**
     * 绘制钟表指针
     */
    private void drawClock(Painter g, int centerX, int centerY) {
        // 绘制简单的钟表指针
        g.fillRect(centerX - 1, centerY - 8, 2, 8);
        g.fillRect(centerX - 1, centerY, 6, 2);
    }
    
    /**
     * 收集道具
     */
    public void collect() {
        collected = true;
    }
    
    /**
     * 获取碰撞矩形
     */
    public Rect getCollisionRect() {
        return new Rect((int) x, (int) y, (int) x + width, (int) y + height);
    }
    
    /**
     * 检查是否超出屏幕
     */
    public boolean isOffScreen() {
        return x < -width;
    }
    
    // Getter方法
    public float getX() { return x; }
    public float getY() { return y; }
    public int getType() { return type; }
    public int getValue() { return value; }
    public boolean isCollected() { return collected; }
    public int getWidth() { return width; }
    public int getHeight() { return height; }
    
    /**
     * 获取道具名称
     */
    public String getName() {
        switch (type) {
            case TYPE_COIN: return "金币";
            case TYPE_SHIELD: return "护盾";
            case TYPE_DOUBLE_SCORE: return "双倍得分";
            case TYPE_SLOW_TIME: return "时间减缓";
            default: return "未知道具";
        }
    }
    
    /**
     * 获取道具描述
     */
    public String getDescription() {
        switch (type) {
            case TYPE_COIN: return "+" + value + "分";
            case TYPE_SHIELD: return "免疫碰撞";
            case TYPE_DOUBLE_SCORE: return "双倍得分" + value + "秒";
            case TYPE_SLOW_TIME: return "时间减缓" + value + "秒";
            default: return "神秘道具";
        }
    }
    
    /**
     * 重新初始化PowerUp（用于对象池复用）
     * @param newX X坐标
     * @param newY Y坐标
     * @param newType 道具类型
     */
    public void reinitialize(float newX, float newY, int newType) {
        this.x = newX;
        this.y = newY;
        this.type = newType;
        this.collected = false;
        this.animationTimer = 0;
        this.pulseScale = 1.0f;
        this.pulsing = true;
        
        // 根据类型重新设置价值
        switch (newType) {
            case TYPE_COIN:
                this.value = 100;
                break;
            case TYPE_SHIELD:
                this.value = 1;
                break;
            case TYPE_DOUBLE_SCORE:
                this.value = 5;
                break;
            case TYPE_SLOW_TIME:
                this.value = 3;
                break;
            default:
                this.value = 50;
        }
    }
    
    /**
     * 重置PowerUp状态（用于对象池回收）
     */
    public void reset() {
        this.x = 0;
        this.y = 0;
        this.type = TYPE_COIN;
        this.collected = false;
        this.animationTimer = 0;
        this.pulseScale = 1.0f;
        this.pulsing = true;
        this.value = 0;
    }
}